﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Widgets.MindMap
{
    public class TitleUpArrowAdorner : Adorner
    {
        /// <summary>
        /// 用以表示“LayoutTitle”被选取状态的装饰器之——上箭头（在各视图中表示的含义不同）。
        /// </summary>
        /// <param name="adornedElement">应传入标题的“MainBorder”主子元素。</param>
        public TitleUpArrowAdorner(UIElement adornedElement, MindMapTitle masterTitle)
            : base(adornedElement)
        {
            this.masterTitle = masterTitle;

            this.Opacity = 0.75;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            //this.SnapsToDevicePixels = true;
            //this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
        }

        private MindMapTitle masterTitle;

        public MindMapTitle MasterTitle
        {
            get { return masterTitle; }
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (this.Visibility != System.Windows.Visibility.Visible || masterTitle == null || masterTitle.MasterView == null) return;

            Brush brush = Brushes.White;
            if (masterTitle != null && masterTitle.MasterView != null)
            {
                switch (masterTitle.MasterView.Theme)
                {
                    case MindMapView.ViewTheme.Light:
                        brush = Brushes.Gray;
                        break;
                    default:
                        brush = Brushes.White;
                        break;
                }
            }
            Pen pen;

            double defaultFontSize = MasterTitle.MasterView.TitleDefaultFontSize;

            if (double.IsNaN(defaultFontSize))
            {
                defaultFontSize = (double)Application.Current.TryFindResource("title_default_font_size");
            }

            double halfDefaultFontSize = defaultFontSize / 2;

            pen = new Pen(brush, 1);

            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);

            adornedElementRect.X -= 6; adornedElementRect.Y -= 6;
            adornedElementRect.Width += 12; adornedElementRect.Height += 12;

            PathGeometry geometry = new PathGeometry();
            Point leftCenter = new Point(adornedElementRect.Left, adornedElementRect.Top + adornedElementRect.Height / 2);
            Point topCenter = new Point(adornedElementRect.Left + adornedElementRect.Width / 2, adornedElementRect.Top - 3);//纵坐标加3是微调的结果
            Point rightCenter = new Point(adornedElementRect.Right, leftCenter.Y);
            Point bottomCenter = new Point(topCenter.X, adornedElementRect.Bottom + 3);//纵坐标加3是微调的结果

            //顶部标记
            PathFigure topPathFigure = new PathFigure() { IsClosed = true, };

            PolyLineSegment topSegment = new PolyLineSegment();
            topPathFigure.StartPoint = new Point(topCenter.X - halfDefaultFontSize, topCenter.Y);
            topSegment.Points.Add(new Point(topCenter.X, topCenter.Y - halfDefaultFontSize));
            topSegment.Points.Add(new Point(topCenter.X + halfDefaultFontSize, topCenter.Y));
            topSegment.Points.Add(new Point(topCenter.X, topCenter.Y - halfDefaultFontSize / 2));
            topPathFigure.Segments.Add(topSegment);

            geometry.Figures.Add(topPathFigure);

            if (geometry.Figures.Count > 0) drawingContext.DrawGeometry(brush, pen, geometry);
        }
    }
}
